package com.smartwebx.ha.web.gis.module.screen;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.rainwater.real.DayevManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.GisWater;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.admin.common.HaUserSys;
import com.smartwebx.ha.web.util.json.module.DateModule;

/**
 * 蒸发
 * 
 * @author tsing
 * 
 */
public class EvaporationScreen extends BasicScreen {
	@Autowired
	private DayevManager dayevManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpSession session;

	/**
	 * 蒸发最新GIS信息
	 * 
	 * @param tm(2011)
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public List<GisWater> getLatestRiverGisInfo(Date tm) {
		HaUserSys loginUser = (HaUserSys) session.getAttribute("haUser");
		HaArea area=haAreaManager.findOne(loginUser.getAreaId());
		List<GisWater> gisWaters = dayevManager.getAllStatDye(tm,area);
		return gisWaters;
	}

	/**
	 * 蒸发单站每日8时过程
	 * 
	 * @param stcd(50103100)
	 *            站码
	 * @param bTm(2010-02-25 )
	 *            开始日期
	 * @param eTm(2011-02-25)
	 *            结束日期
	 * @param dxn(2011)
	 *            典型年
	 * @return
	 * @throws ParseException
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		// 蒸发日过程
		List<DateValue> dateValues = dayevManager.getSingleStatDyeDayProcess(
				stcd, bTm, eTm);
		// 典型年
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = dayevManager
				.getSingleStatDyeDayProcess(stcd, startDate, endDate);
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		for (DateValue dateValue : dateValues) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValue);
		/*	viewProcessBean.setDate(dateValue.getTm());
			viewProcessBean.setValue(dateValue.getVal());*/
			for (DateValue dxndateValue : dxnDateValues) {
				String s1 = sdf.format(dateValue
						.getTm());
				String s2 = sdf.format(dxndateValue
						.getTm());
				if (s1.equals(s2)) {
					viewProcessBean.setValue6(dxndateValue.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;
	}
	
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		// 蒸发日过程
		List<DateValue> dateValues = dayevManager.getSingleStatDyeDayProcess(
				stcd, bTm, eTm);
		// 典型年
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = dayevManager
				.getSingleStatDyeDayProcess(stcd, startDate, endDate);
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<Double> vals=Lists.newArrayList();//日蒸发量过程
		List<Double> dxnVals=Lists.newArrayList();//典型年
		for (DateValue dateValue : dateValues) {
			vals.add(dateValue.getVal());
			for (DateValue dxndateValue : dxnDateValues) {
				String s1 = sdf.format(dateValue
						.getTm());
				String s2 = sdf.format(dxndateValue
						.getTm());
				if (s1.equals(s2)) {
					dxnVals.add(dxndateValue.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("vals", vals);
		result.put("dxnVals", dxnVals);
		return result;
	}
}
